﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace M2MqttWinForm
{
    public partial class Form1 : Form
    {
        private delegate void dg_textBox_Receive(string s);
        private dg_textBox_Receive dg_r = null;

        public Form1()
        {
            InitializeComponent();
            // 委托
            dg_r = new dg_textBox_Receive(AddToTextBox_Receive);
            // 实例化Mqtt客户端
            client = new MqttClient(textBox_Host.Text);
            // 注册接收消息事件 
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            
        }
        private MqttClient client;

        
        /// <summary>
        /// 委托显示TextBox_Receive
        /// </summary>
        /// <param name="s"></param>
        private void AddToTextBox_Receive(string s)
        {
            try
            {
                if (textBox_Receive != null && !textBox_Receive.IsDisposed)
                {
                    if (textBox_Receive.InvokeRequired)
                    {
                        dg_textBox_Receive d = AddToTextBox_Receive;
                        textBox_Receive.Invoke(d, s);
                    }
                    else
                    {
                        textBox_Receive.AppendText(s);
                        if (textBox_Receive.Lines.GetUpperBound(0) > 5000)
                        {
                            textBox_Receive.Text = "";
                        }
                    }
                }
            }
            catch (Exception unerr) { Console.WriteLine(unerr); }
        }

        void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            string s = string.Format("subscriber,topic:{0},content:{1}", e.Topic, Encoding.UTF8.GetString(e.Message));
            s += "\r\n";
            // 打印订阅的发布端消息
            Console.WriteLine(s);
            dg_r(s);
        }

        private void button_Publish_Click(object sender, EventArgs e)
        {
            string content = textBox_Publish.Text;
            string strValue = Convert.ToString(content);

            // 发布消息主题 "/home/temperature" ，消息质量 QoS 2 
            client.Publish(textBox_Topic.Text, Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
        }

        private void button_Connect_Click(object sender, EventArgs e)
        {
            if (client == null)
            {
                MessageBox.Show("MqttClient = null, 可能尚未实例化", "Exception 出现异常");
                return;
            }
            if (client.IsConnected)
            {
                try
                {
                    client.Disconnect();
                    button_Connect.Text = "Connect 连接";
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message, "Exception 出现异常");
                }
            }
            else
            {
                string clientId = Guid.NewGuid().ToString();
                try
                {
                    client.Connect(clientId);
                    button_Connect.Text = "Disconnect 断开连接";
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message, "Exception 出现异常");
                }
            }
        }

        private void button_Subscribe_Click(object sender, EventArgs e)
        {
            // 订阅主题 "/home/temperature"， 订阅质量 QoS 2 
            client.Subscribe(new string[] { textBox_Topic.Text }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
        }
    }
}
